from leetcode_test import *

"""
给定一个二叉树和一个目标和，判断该树中是否存在根节点到叶子节点的路径，这条路径上所有节点值相加等于目标和。

说明: 叶子节点是指没有子节点的节点。

示例: 
给定如下二叉树[5,4,8,11,null,13,4,7,2,null,null,null,1]，以及目标和 sum = 22 返回True
"""


class Solution:
    """
    不能用 sum == 0 返回，
    """
    def hasPathSum(self, root: TreeNode, sum: int) -> bool:
        if root is None:
            return False
        if root.val == sum and not root.left and not root.right:
            return True
        return self.hasPathSum(root.left, sum - root.val) or self.hasPathSum(
            root.right, sum - root.val
        )


def test():
    t = TreeNode.create([5, 4, 8, 11, None, 13, 4, 7, 2, None, None, None, 1])
    assert Solution().hasPathSum(t, 22)
    assert Solution().hasPathSum(t, 999) == False


def test1():
    t = TreeNode.create([1, 2])
    assert Solution().hasPathSum(t, 1) == False
    assert Solution().hasPathSum(t, 3)


def test3():
    t = TreeNode.create([])
    assert Solution().hasPathSum(t, 0) == False
